<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Complete Interaction of the measures
 *
 * @author febusch
 *
 * 28.11.2012 Migration to Gas ORM by lukas
 *
 *
 */
class Measure extends TMT_Controller {

    function __construct() {
        parent::__construct();

        $this->load->helper('url', 'input', 'form');
        $this->load->library('datepicker');
        #$this->load->model('data_management/measure_model');
    }

    # works with gas orm
    public function index() 
    {
        $this->headline = 'Übersicht über alle aktuellen Schulungsmassnahmen';
        #$this->view_data['measures'] = $this->measure_model->get_overview();

        // overview erstellen
        $this->view_data['measures'] = Model\Training::all();

        foreach ($this->view_data['measures'] as $measure) {
            $this->view_data['amount_participants'] = count($measure->employee());
        }
        


    }
    

    public function create() {
    	$course_id = $this->input->post('course_id');
        if ($course_id) {

            // get specific course data
            $this->view_data['course']  = Model\Course::find($course_id);

            /*$locations = NULL;

            // iterate trough listed employees and save the location information
            foreach ($this->view_data['course']->employee()->contactdata()->place()->city as $city) {
                if (!in_array($city, $locations)){
                    $locations[$city]++;
                }
                else {
                    $locations[$city] = 1;
                }
            }

            // If more than one location is available, save it in a session var
            if(sizeof($locations > 1) $this->session->set_flashdata('loc_conflict', TRUE);

            $this->view_data['locations'] = $locations;
            */ 

            #$this->view_data['amount_participants'] = sizeof($this->view_data['course']->stopedemand()->employee());
            


            $this->view_data['target']              = 'measure/confirm/' . $course_id;
            $this->view_data['hidden']              = array('course_id' => $course_id);
            
            // Test data
            $this->view_data['locations']['Darmstadt']      = 5;
            $this->view_data['locations']['Bonn']           = 2;
            $this->view_data['locations']['Eschborn']       = 2;
            $this->view_data['locations']['Bielefeld']      = 2;
            $this->view_data['locations']['Frankfurt']      = 2;

            $this->view_data['amount_participants']         = 13;

            $this->session->set_flashdata('loc_conflict', TRUE);

        }

        $this->headline = 'Eine neue Maßnahme erstellen';
    }

    public function confirm($course_id = FALSE) 
    {
        if($course_id)
        {
            $this->view_data['course']       = Model\Course::with('team', 'contactdata', 'place', 'participant')->find($course_id);
            $this->view_data['participants'] = $this->view_data['course']->participant();
            $this->view_data['location']     = $this->input->post('location');
            $this->view_data['loc_conflict'] = $this->session->flashdata('loc_conflict');
            
            $this->session->set_flashdata('CourseID', $course_id);
            $this->session->set_flashdata('Location', $this->input->post('location'));
            $this->session->set_flashdata('Date', $this->datepicker->get_values('%Y-%m-%d %H:%M:%S')); 
        }
        
        $this->headline = 'Eine neue Maßnahme erstellen - Zusammenfassung';
    }

    /* DETAILS
     *
     * Wird nach dem Klick auf eine bestimmte Schulung aufgerufen
     * und liefert alle Schulungsspezifischen Daten.
     *
    */
    public function details($measureID = FALSE) {
                
        $mode = $this->input->get('mode');
        $this->view_data['display'] = $this->input->get('display');
        $this->headline = 'Maßnahmedetails #' . $measureID;

        // get specific training information
        $this->view_data['measure']             = Model\Training::with('employee', 'contactdata')->find($measureID); 

        // count participants in current training
        $this->view_data['amount_participants'] = count(Model\Training::find($measureID)->employee());

        // =======================================
        // EDIT mode condition start
        // if edit IS active then
        // =======================================
        if($mode == 'edit')
        {
            $this->view_data['edit'] = TRUE;
            $stati                   = Model\Trainingstatus::all();

            // stupid dropdown needs array, so here we go
            foreach ($stati as $status) {
                $this->view_data['status_options'][$status->id] = $status->name;
            }
            
            // get all employees regardless of being listed/unlisted
            $employees = Model\Employee::with('contactdata')->all();

            // get all employees that are listed in the training
            $members   = Model\Training::find($measureID)->employee(); 

            // ARRAY BUILDER
            // stupid multi-select needs array, so here we go
            if (count($members) <> 0 ){  # there are employees who are listed
                foreach ($members as $member) {
                    // add employee to listed ones
                    $this->view_data['employees_listed'][$member->id] = $member->contactdata()->forename . ', ' . $member->contactdata()->surname;
                }
                if (count($employees) <> 0){ #there are employees who are not listed
                    foreach ($employees as $employee) {
                        // check if the employee is already listed
                        if (!array_key_exists($employee->id, $this->view_data['employees_listed'])){
                         // add employee to unlisted ones
                            $this->view_data['employees_unlisted'][$employee->id] = $employee->contactdata()->forename . ', ' . $employee->contactdata()->surname;
                        }
                    }
                }
                else { #there aren't any employees available that are not listed
                    $this->view_data['employees_unlisted'] = NULL;
                }
            }
            else{   # there are no listed employees
                $this->view_data['employees_listed'] = NULL;

                foreach ($employees as $employee) {
                    $this->view_data['employees_unlisted'][$employee->id] = $employee->contactdata()->forename . ', ' . $employee->contactdata()->surname;
                }
            } 
        }
        // edit IS NOT active
        else 
            $this->view_data['edit'] = FALSE;

        // end of EDIT condition
    }
    
    public function do_it()
    {
        $this->load->model('data_management/measure_model');
        
        $ft_id = $this->session->flashdata('FachthemenID');
        $loc   = $this->session->flashdata('Standort');
        $date  = $this->session->flashdata('Termin');
        $memb  = $this->session->flashdata('Members');
        
        if($ft_id AND $loc AND $date AND $memb)
        {
            if ($this->measure_model->create_measure($ft_id, $loc, $date, $memb))
                $this->display_error ('Erfolg', 'Maßnahme wurde erfolgreich erstellt!', 'success');
            else {
                $this->display_error ('Ein Fehler ist aufgetreten', 'Maßnahme konnte nicht erstellt werden!');
            }
        }
        
    }
    
    public function edit($id = FALSE)
    {
        if($id)
        {
            $new_status = $this->input->post('status_select');
            if($new_status)
            {
                $this->measure_model->update_status($new_status, $id);
            }
        }
        redirect('/measure/details/'.$id);
    }
    
    public function edit_members($measureID = FALSE)
    {
        $to_add = $this->input->post('member_add'); // Array von hinzuzufügenden Teilnehmern
        $to_del = $this->input->post('member_del'); // Array von zu löschenden Teilnehmern
        var_dump($to_del);
        
        if($to_add)
        {
            $this->measure_model->create_relations_mitarbeiter_smassnahme($measureID, $to_add);
        }
        elseif($to_del)
        {
            $this->measure_model->delete_relations_mitarbeiter_smassnahme($measureID, $to_del);
        }
        
        redirect('/measure/details/'.$measureID.'?mode=edit');
    }
    
    
    
    
    
    
    
    
    /*
     * 
     *          ACHTUNG TRASH CODE
     *          FUNKTIONIERT NOCHT NICHT!
     * 
     */
    
    
    public function exclude_member($id = FALSE)
    {
//        if($id)
//        {
//            $members = $this->session->userdata('measure_members');
//            foreach($members as $member)
//            {
//                if($member->persnr != $id)
//                {
//                    $new_data[] = $member;
//                }
//            }
//            $this->session->unset_userdata('measure_members');
//            $this->session->set_userdata('measure_members', $new_data);
//        }
//        
//        $this->session->keep_flashdata('location');
        
        redirect('/measure/confirm/'.$this->session->flashdata('measureid'));
    }
    
    private function _validate_exclude()
    {
        echo $this->input->get('exclude');
    }
}
